package com.xx.web.controller.system;

import com.xx.common.constant.BridgeAlarm;
import com.xx.common.constant.BridgeHealthInfo;
import com.xx.common.constant.BridgeSpecialEvent;
import com.xx.system.service.ISysBridgeAlarmService;
import com.xx.system.service.ISysBridgeHealthInfoService;
import com.xx.system.service.ISysBridgeSpecialEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 * 桥梁信息相关接口
 * @Author kzw
 * @Date 2025/4/2 14:07
 */

@RestController
@RequestMapping("/api")
public class SysBridgeAlarmController {

    @Autowired
    private ISysBridgeAlarmService bridgeAlarmService;

    @Autowired
    private ISysBridgeHealthInfoService bridgeHealthInfoService;

    @Autowired
    private ISysBridgeSpecialEventService specialEventService;

    // 接收报警信息同步接口请求
    @PostMapping("/bridgeWarningInfo")
    public Map<String, Object> syncBridgeAlarm(@RequestBody Map<String, List<BridgeAlarm>> request) {
        Map<String, Object> response = new HashMap<>();
        List<BridgeAlarm> alarmList = request.get("data");

        if (alarmList == null || alarmList.isEmpty()) {
            response.put("code", 202);
            response.put("msg", "数据格式错误");
            response.put("data", null);
            return response;
        }

        boolean success = bridgeAlarmService.syncBridgeAlarmInfo(alarmList);
        if (success) {
            response.put("code", 200);
            response.put("msg", "报警信息同步成功");
            response.put("data", null);
        } else {
            response.put("code", 203);
            response.put("msg", "报警信息同步失败");
            response.put("data", null);
        }
        return response;
    }

    /**
     * 同步桥梁健康度信息
     * @param request 桥梁健康度信息列表
     * @return 同步是否成功
     */
    @PostMapping("/bridgeHealthInfo")
    public Map<String, Object> syncBridgeHealthInfo(@RequestBody Map<String, List<BridgeHealthInfo>> request) {
        Map<String, Object> response = new HashMap<>();
        List<BridgeHealthInfo> bridgeHealthInfoList = request.get("data");

        if (bridgeHealthInfoList == null || bridgeHealthInfoList.isEmpty()) {
            response.put("code", 202);
            response.put("msg", "数据格式错误");
            response.put("data", null);
            return response;
        }

        boolean success = bridgeHealthInfoService.syncBridgeHealthInfo(bridgeHealthInfoList);
        if (success) {
            response.put("code", 200);
            response.put("msg", "桥梁健康度信息同步成功");
            response.put("data", null);
        } else {
            response.put("code", 203);
            response.put("msg", "桥梁健康度信息同步失败");
            response.put("data", null);
        }
        return response;
    }

    /**
     * 同步特殊事件信息
     * @param
     * @param token 认证token
     * @return 同步是否成功
     */
    @PostMapping("/specialEventInfo/sync")
    public Map<String, Object> syncSpecialEvent(@RequestBody Map<String, List<BridgeSpecialEvent>> request,
                                                @RequestHeader("bridgeToken") String token) {
        Map<String, Object> response = new HashMap<>();
        List<BridgeSpecialEvent> eventList = request.get("data");

        if (eventList == null || eventList.isEmpty()) {
            response.put("code", 202);
            response.put("msg", "数据格式错误");
            response.put("data", null);
            return response;
        }

        boolean success = specialEventService.syncSpecialEvent(eventList, token);
        if (success) {
            response.put("code", 200);
            response.put("msg", "特殊事件信息同步成功");
            response.put("data", null);
        } else {
            response.put("code", 201);
            response.put("msg", "特殊事件信息同步失败，Token验证失败或其他错误");
            response.put("data", null);
        }
        return response;
    }

}
